package com.helloworld.controller;

import com.helloworld.service.UserService;
import com.helloworld.vo.HttpResponse;
import com.helloworld.vo.User.UserAddReq;
import com.helloworld.vo.User.UserListReq;
import com.helloworld.vo.User.UserUpdateReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("用户查询")
    @GetMapping({"/Info"})
    public HttpResponse queryUserById(Integer id){
        return HttpResponse.suc(userService.queryUserById(id));
    }

    @ApiOperation("用户列表")
    @GetMapping({"/list"})
    public HttpResponse queryUserList(UserListReq req){
        return HttpResponse.suc(userService.queryUserList(req.getNameLike()));
    }

    @ApiOperation("用户添加")
    @PostMapping({"/add"})
    public HttpResponse addUser(@RequestBody UserAddReq req){
        Integer id = userService.addUser(req);
        return HttpResponse.suc(userService.queryUserById(id));
    }

    @ApiOperation("用户更新")
    @PostMapping({"/update"})
    public HttpResponse updateUser(@RequestBody UserUpdateReq req){
        userService.updateUser(req);
        return HttpResponse.suc(userService.queryUserById(req.getId()));
    }

    @ApiOperation("用户删除")
    @PostMapping({"/delete"})
    public HttpResponse deleteUser(@RequestBody Integer id){
        userService.deleteUser(id);
        return HttpResponse.suc(null);
    }
}
